package com.huihui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.bean.MineZan;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.ShareUtils;
import com.huihui.utils.ToolUtils;
import com.huihui.widget.FullScreenVideoView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Call;


/**
 * Created by Administrator on 2018/8/18.
 * 我的点赞适配器
 */

public class ZanAdapter extends RecyclerView.Adapter<ItemViewHolder> {

    private Context context;
    private List<MineZan> list;
    private MineZanGridViewAdapter adapter;

    public ZanAdapter(Context context, List<MineZan> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_zan, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, int position) {
        final MineZan zan = list.get(position);

        final int class_id = zan.getClass_id();
        LoadImageUtil.displayImage(context, zan.getAuthor_head_img(), holder.head_view);
        holder.user_name.setText(zan.getAuthor_nickname());
        holder.user_time.setText(zan.getTime());
        holder.desc.setText(zan.getContent());
        holder.zan.setText(String.valueOf(zan.getCollection_num()));
        holder.pinglun.setText(String.valueOf(zan.getComment_num()));
        holder.zhuanfa.setText(String.valueOf(zan.getShare_num()));
        holder.gridview.setVisibility(View.VISIBLE);
        holder.video_layout.setVisibility(View.GONE);
        if (class_id == 0) {
            //商品
            adapter = new MineZanGridViewAdapter(zan.getContent_url(), context);
            holder.gridview.setAdapter(adapter);

        } else if (class_id == 1) {
            holder.gridview.setVisibility(View.GONE);
            holder.video_layout.setVisibility(View.VISIBLE);
            holder.video_view.setVisibility(View.GONE);
            holder.video_view.setVideoURI(Uri.parse(zan.getContent_url().get(0)));
            LoadImageUtil.displayImagePreview(context, zan.getThumbnail(), holder.img_thumb, R.color.color_e9e9e9, false);
            //视频
        } else if (class_id == 2) {
            //文章
            adapter = new MineZanGridViewAdapter(zan.getContent_url(), context);
            holder.gridview.setAdapter(adapter);
        }
        holder.fenxiangLayout.setTag(position);
        holder.fenxiangLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                showSharePop(v, p);
            }
        });
        final int isLike = zan.getIs_like();
        if (isLike == 0) {
            holder.zan_icon.setImageResource(R.mipmap.zan);
        } else {
            holder.zan_icon.setImageResource(R.mipmap.zan_press);
        }
        holder.item_layout.setTag(position);
//        if (zan.getClass_id() == 1) {
//            Toast.makeText(context,"视频页面还没有设计",Toast.LENGTH_SHORT).show();
//        } else {
        holder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                if (class_id == 1) {
                    IntentUtils.gotoSmallVideoInfoActivity(context, zan.getRelation_id());
                } else {

                    IntentUtils.gotoDetialAcitvity(context, list.get(p), p);
                }
            }
        });
//        }
        holder.dianzan_layout.setTag(position);
        holder.dianzan_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                if (isLike == 1) {
                    removeZan(p);
                } else {
                    addZan(p);
                }
            }
        });

//        holder.video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mp) {
//                holder.play.setVisibility(View.VISIBLE);
//                holder.img_thumb.setVisibility(View.VISIBLE);
//            }
//        });

//        holder.video_view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //播放或者暂停
//                if (holder.play.getVisibility() == View.VISIBLE) {
//                    holder.play.setVisibility(View.INVISIBLE);
//                    holder.video_view.start();
//                    holder.img_thumb.setVisibility(View.GONE);
//                } else {
//                    holder.play.setVisibility(View.VISIBLE);
//                    holder.video_view.pause();
//                }
//
//            }
//        });
//        holder.video_view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mp) {
//                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
//                    @Override
//                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
//                        mVideoWidth = mp.getVideoWidth();
//                        mVideoHeight = mp.getVideoHeight();
//                        showVideoHeight = holder.video_view.getHeight();
//                        showViewWidth = holder.video_view.getWidth();
//                        scaleHeight = (float) mVideoHeight / (float) showVideoHeight;
//                        scaleWidth = (float) mVideoWidth / (float) showViewWidth;
//                        if (scaleWidth > scaleHeight) {
//                            scale = scaleWidth;
//
//                        } else {
//                            scale = scaleHeight;
//                        }
//                        scale = scaleWidth;
//
////                        scale = (float) mVideoWidth / (float) mVideoHeight;
//                        refreshPortraitScreen(holder.video_view, showVideoHeight == 0 ? ToolUtils.dp2px(context, 300) : showVideoHeight
//                                , showViewWidth == 0 ? ToolUtils.dp2px(context, 300) : showViewWidth);
//                    }
//                });
//            }
//        });
//        holder.img_thumb.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //播放或者暂停
//                if (holder.play.getVisibility() == View.VISIBLE) {
//                    holder.play.setVisibility(View.INVISIBLE);
//                    holder.video_view.start();
//                    holder.img_thumb.setVisibility(View.GONE);
//                } else {
//                    holder.play.setVisibility(View.VISIBLE);
//                    holder.video_view.pause();
//                }
//
//            }
//        });
    }

    private void addZan(int p) {
        //商品
        final MineZan mineZan = list.get(p);
        int class_id = mineZan.getClass_id();
        int relation_comment_id = 0;
        long relation_id = mineZan.getRelation_id();
        OkHttpRequest.getInstance().addCollection(HttpContants.addCollection,
                AppManager.getUserInfo().getUser_id(),
                relation_id,
                class_id,
                relation_comment_id,
                new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
//                                currentPage = 1;
//                                getProductComment();
                                mineZan.setIs_like(1);
                                mineZan.setCollection_num(mineZan.getCollection_num() + 1);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void removeZan(final int p) {
        final MineZan mineZan = list.get(p);
        int class_id = mineZan.getClass_id();
        int comment_id = 0;

        OkHttpRequest.getInstance().removeCollection(HttpContants.removeCollection, AppManager.getUserInfo().getUser_id(),
                mineZan.getRelation_id(), class_id, comment_id, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                int collection_num = mineZan.getCollection_num();
                                mineZan.setCollection_num(--collection_num);
                                mineZan.setIs_like(0);
                                notifyItemChanged(p);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void showSharePop(View layout, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_share_layout, null);
        final PopupWindow pop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        pop.setOutsideTouchable(true);
        pop.setFocusable(true);
        pop.setBackgroundDrawable(new ColorDrawable());
        pop.showAtLocation(layout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        view.findViewById(R.id.pop_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        view.findViewById(R.id.wechat_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop, position, 0);
            }
        });

        view.findViewById(R.id.wechat_cicle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send(pop, position, 1);
            }
        });
    }

    private void send(final PopupWindow pop, final int position, final int type) {
        pop.dismiss();
        //获取链接
        OkHttpRequest.getInstance().getShareUrl(HttpContants.SHARE_URL, AppManager.getUserInfo().getUser_id(), list.get(position).getClass_id(), list.get(position).getRelation_id(), new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject != null) {
                        String url = jsonObject.getString("url");
                        if (list.get(position).getClass_id() == 0) {
                            ShareUtils.shareSelect((Activity) context, url, type, list.get(position).getContent(), "信息来自好价," + list.get(position).getAuthor_nickname());
                        } else if (list.get(position).getClass_id() == 1) {
                            ShareUtils.shareSelect((Activity) context, url, type, list.get(position).getContent(), "信息来自视频," + list.get(position).getAuthor_nickname());
                        } else {
                            ShareUtils.shareSelect((Activity) context, url, type, list.get(position).getContent(), "信息来自讨论," + list.get(position).getAuthor_nickname());
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((BaseActivity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((BaseActivity) context).getWindow().setAttributes(lp);
        ((BaseActivity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    int showVideoHeight = 0;
    int showViewWidth = 0;
    int mVideoHeight = 0;
    int mVideoWidth = 0;
    float scale = 0;
    float scaleWidth = 0;
    float scaleHeight = 0;

    //重新刷新 竖屏显示的大小  树屏显示以宽度为准
    public void refreshPortraitScreen(FullScreenVideoView mVideoView, int height, int width) {
        if (height == 0) {
            height = showVideoHeight;
        }
        if (mVideoHeight > 0 && mVideoWidth > 0) {

            mVideoWidth = (int) (mVideoWidth / scale);
            mVideoHeight = (int) (mVideoHeight / scale);
            mVideoView.getHolder().setFixedSize(mVideoWidth, mVideoHeight);
            mVideoView.setMeasure(mVideoWidth, mVideoHeight);
            mVideoView.requestLayout();
        }
    }
}
